<template>
  <div>
    <div class="content">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
      >
        <cell-view v-for="item in collectList" :key="item.id" :msg="item" />
      </van-list>
    </div>
  </div>
</template>

<script>
import CellView from '@/components/cell.vue'
import { ListApi } from '@/api/list'
export default {
  name: 'ListView',
  props: {
  },
  components: {
    CellView
  },
  data () {
    return {
      collectList: [],
      loading: false,
      finished: false,
      error: false,
      current: 1, // 当前页
      pageSize: 10 // 每页数量
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onLoad () {
      try {
        const { data: { rows } } = await ListApi({
          optType: 2
        })
        this.collectList = rows
        if (rows.length < this.pageSize) {
          this.finished = true
          return
        }
        // 测试bug
        // if (Math.random() * 10 > 2) {
        //   throw new Error('出问题')
        // }
        this.collectList.push(...rows)
        this.current += 1
        this.loading = false
      } catch (error) {
        this.error = true
        this.loading = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  padding-top: 100px;
}
</style>
